package com.chenjie.crud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenjie.crud.dto.common.DeptDto;
import com.chenjie.crud.dto.request.query.DeptIdReq;
import com.chenjie.crud.dto.request.query.DeptReqQuery;
import com.chenjie.crud.enums.DeleteFlagEnum;
import com.chenjie.crud.eo.Dept;
import com.chenjie.crud.mapper.DeptMapper;
import com.chenjie.crud.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author chenjie
* @description 针对表【dept】的数据库操作Service实现
* @createDate 2022-12-05 15:46:27
*/
@Service
@Slf4j
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
    implements DeptService{

    @Resource
    private DeptMapper deptMapper;

    @Override
    @Transactional
    public Long createDept(DeptReqQuery deptReqQuery) {
        log.info("createDept deptReqQuery:{}",deptReqQuery);
        DeptDto deptDto = new DeptDto();
        BeanUtil.copyProperties(deptReqQuery,deptDto);
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        boolean save = this.save(dept);
//        int insert = deptMapper.insert(dept);
        return dept.getId();
    }

    @Override
    public List<Dept> selectAllDept(DeptReqQuery deptReqQuery) {
        QueryWrapper<Dept> objectQueryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(deptReqQuery.getDeptName())){
            objectQueryWrapper.like("dept_name",deptReqQuery.getDeptName());
        }
        if(StrUtil.isNotEmpty(deptReqQuery.getDeptName())){
            objectQueryWrapper.eq("delete_flag",deptReqQuery.getDeleteFlag());
        }
        List<Dept> depts = deptMapper.selectList(objectQueryWrapper);
        return depts;
    }

    @Override
    public Dept selectDeptbyId(DeptIdReq deptIdReq) {
        Dept dept = deptMapper.selectById(deptIdReq.getId());
        return dept;
    }

    @Override
    @Transactional
    public Boolean updateDeptbyId(DeptReqQuery deptReqQuery) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptReqQuery,dept);
        int i = deptMapper.updateById(dept);
        return i>0;
    }

    @Override
    @Transactional
    public Boolean deleteDeptbyId(DeptIdReq deptIdReq) {
        int i = deptMapper.deleteById(deptIdReq.getId());
        return i>0;
    }

    public static void main(String[] args) {
        DateTime dateTime = new DateTime();
        System.out.println(dateTime);
    }
}




